<?php
//рус
include_once(_APP_PATH. "business/paginator/paginator_mapper.php");
class paginator_controlls_http_web extends controller_prototype_class
{
    protected $_template = '_paginator.phtml';
    /**
		шаблон ссылок.
		подставляется переменная $_pageVar
		пример: http://.../?page=__page__
     */
    protected $_pattern;
    /**
		переменная страницы для подстановки
     */
    protected $_pageVar = '__page__';
    
    private $_mapper;

    public function GetMapper()
    {
    	if (!$this->_mapper) $this->_mapper = PaginatorMapper::GetInstance();
    	return $this->_mapper;
    }
    
    public function SetTemplate ($template = false)
    {
        if ($template) {
            $this->_template = $template;
        } else {
            $this->_template = '_paginator.phtml';
        }
        return $this;
    }
    public function SetUrlPattern ($pattern)
    {
        $this->_pattern = $pattern;
        return $this;
    }
    public function Generate ()
    {
        //if ($this->GetMapper()->GetTotalPages() < 2) return '';
        $t = $this->GetTemplator();
        $t->page = $this->GetMapper()->GetPage();
        $t->totalPages = $this->GetMapper()->GetTotalPages();
        if ($t->page > $t->totalPages) $t->page = $t->totalPages + 1;  
        $t->pattern = $this->_pattern;
        $t->pageVar = $this->_pageVar;
        return $t->render('paginator/' . $this->_template);
    }
    
    public function IsLastPage()
    {
        return $this->GetMapper()->GetPage() >= $this->GetMapper()->GetTotalPages(); 
    }
    
    public function GetNextPageLink()
    {
        if ($this->IsLastPage()) {
            $nextPage = $this->GetMapper()->GetTotalPages();
        } else {
            $nextPage = $this->GetMapper()->GetPage()+1;
        }
      
        return str_replace($this->_pageVar, $nextPage, $this->_pattern);
    }
    
    public function IsFirstPage()
    {
        return $this->GetMapper()->GetPage() <= 1; 
    }    
    
    public function GetPrevPageLink()
    {
        if ($this->IsFirstPage()) {
            $prevPage = 1;
        } else {
            $prevPage = $this->GetMapper()->GetPage()-1;
        } 

        return str_replace($this->_pageVar, $prevPage, $this->_pattern);
    }    
}